<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="" prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入客户名称/林客ID/直播ID" clearable style="width: 260px;" />
        </el-form-item>
        <el-form-item label="直播日期：">
          <el-date-picker clearable v-model="date" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
            range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime" placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="直播状态：" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in statusList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统计日期：">
          <el-date-picker clearable v-model="countDate" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
            range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeCountTime"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head" style="margin: 0 0 20px">
        <div>
          <el-button type="primary" @click="salesUpLoad">导入直播数据</el-button>
          <el-button type="primary" @click="writeOffUpLoad">导入订单数据</el-button>
        </div>
        <div>
          <el-button type="primary" @click="seeOrderDialog">查看导入订单数据</el-button>
          <el-button type="primary" @click="seeLiveDialog">查看导入直播数据</el-button>
          <export-excel-button title="直播数据" action="/live/import/data/export" :params="queryParams" />
        </div>
      </div>
      <el-table v-loading="loading" ref="multipleTable" :data="tabList" stripe border tooltip-effect="dark"
        style="width: 100%" show-summary :summary-method="getSummaries">

        <el-table-column label="序号" align="center" type="index" width="64px" />
        <el-table-column label="直播状态" align="center" prop="status"></el-table-column>
        <el-table-column label="直播日期" align="center" prop="liveDate" width="120px"></el-table-column>
        <el-table-column label="直播时间" align="center" prop="liveTime" width="120px"></el-table-column>
        <el-table-column label="直播间" align="center" prop="roomName" width="120px"></el-table-column>
        <el-table-column label="直播客户" align="center" prop="customerName"></el-table-column>
        <el-table-column label="林客ID" align="center" prop="linkId"></el-table-column>
        <el-table-column label="商家信息" align="center" prop="merchantInfo"></el-table-column>
        <el-table-column label="直播主播" align="center" prop="anchor"></el-table-column>
        <el-table-column label="直播中控" align="center" prop="centerControl"></el-table-column>
        <el-table-column label="直播ID" align="center" prop="liveId" width="178px"></el-table-column>
        <el-table-column label="直播开始时间" align="center" prop="liveBeginTime" width="155px"></el-table-column>
        <el-table-column label="直播结束时间" align="center" prop="liveEndTime" width="155px"></el-table-column>
        <el-table-column label="直播时长" align="center" prop="liveHour" width="120px"></el-table-column>
        <el-table-column label="支付GMV" align="center" prop="payGmv" width="90px"></el-table-column>
        <el-table-column label="支付券数" align="center" prop="payOfCoupons"></el-table-column>
        <el-table-column label="统计日期" align="center">
          <template slot-scope="{}" slot="header">
            <span v-if="statisticsDate">统计日期 ({{ statisticsDate }})</span>
            <span v-else>统计日期</span>
          </template>
          <el-table-column label="核销GMV" align="center" prop="writeOffGmv" width="90px"></el-table-column>
          <el-table-column label="退款GMV" align="center" prop="refundGmv" width="90px"></el-table-column>
          <el-table-column label="核销券数" align="center" prop="writeOffCoupon"></el-table-column>
          <el-table-column label="退款券数" align="center" prop="refundCoupon"></el-table-column>
          <el-table-column label="服务商佣金" align="center" prop="serviceCommission" width="100px"></el-table-column>
          <el-table-column label="直播提成" align="center" prop="liveCommission"></el-table-column>
          <el-table-column label="直播收入" align="center" prop="liveIncome"></el-table-column>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="120px">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editClick(scope.row)">关联</el-button>
            <el-button type="text" size="small" @click="configClick(scope.row)">佣金配置</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" :page-sizes="[50, 100, 300, 500, 1000]"></pagination>
    </div>

    <!-- 关联弹窗 -->
    <IdLiveDialog v-if="dialogVisible" :id="dialogId" :pathType="pathType" :visible.sync="dialogVisible"
      @success="_handleSuccess">
    </IdLiveDialog>
    <!-- 佣金配置 -->
    <ConfigDialog v-if="configVisible" :id="dialogId" :pathType="pathType" :visible.sync="configVisible"
      @success="_handleSuccess">
    </ConfigDialog>
    <!-- 导入数据弹窗 -->
    <ImportDialog v-if="importVisible" :id="dialogId" :pathType="pathType" :visible.sync="importVisible"
      @success="_handleSuccess">
    </ImportDialog>
    <!-- 查看导入直播数据弹窗 -->
    <SeeLiveDialog v-if="liveVisible" :id="dialogId" :visible.sync="liveVisible" @success="_handleSuccess">
    </SeeLiveDialog>
    <!-- 查看导入订单数据弹窗 -->
    <SeeOrderDialog v-if="orderVisible" :id="dialogId" :visible.sync="orderVisible" @success="_handleSuccess">
    </SeeOrderDialog>
  </div>
</template>

<script>
import {
  dataList,
} from "@/api/customer/serviceData";
import IdLiveDialog from "./components/idLiveDialog";
import ConfigDialog from "./components/configDialog";
import ImportDialog from "./components/importDialog";
import SeeLiveDialog from "./components/seeLiveDialog";
import SeeOrderDialog from "./components/seeOrderDialog";
import ExportExcelButton from "@/components/ExportExcelButton/index";
export default {
  name: "",
  components: {
    SeeLiveDialog,
    SeeOrderDialog,
    ImportDialog,
    IdLiveDialog,
    ConfigDialog,
    ExportExcelButton
  },
  data() {
    return {
      dialogId: '',
      dialogVisible: false,
      configVisible: false,
      importVisible: false,
      liveVisible: false,
      orderVisible: false,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      // 总条数
      total: 0,
      //表格数据
      tabList: [],
      pathType: 'add',
      date: [],
      countDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        beginTime: '',
        endTime: '',
        keywords: '',
        status: '',
        countBeginTime: '',
        countEndTime: '',
      },
      statisticsDate: '',
      shopType: "",
      statusList: [
        { id: '', value: '全部' },
      ],
    };
  },
  computed: {

  },
  created() {
    this.getDict()
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      dataList(this.queryParams).then(response => {
        response.result.records.forEach(item => {

          if (item.status == 1) {
            item.status = '进行中'
          }
          if (item.status == 2) {
            item.status = '已完成'
          }
        })
        this.tabList = response.result.records;

        this.total = response.result.total;
        this.loading = false;

      });
    },

    //获取状态字典
    getDict() {
      var dictType = "live_calendar_status";
      this.$getSelectList(dictType)
        .then((res) => {
          this.statusList = this.statusList.concat(res.result[0].dictList)
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //时间
    changeTime(e) {
      this.queryParams.beginTime = e[0]
      this.queryParams.endTime = e[1]
    },
    //时间
    changeCountTime(e) {
      this.queryParams.countBeginTime = e[0]
      this.queryParams.countEndTime = e[1]
    },
    // 表单重置
    reset() {
      this.date = []
      this.countDate = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        beginTime: '',
        endTime: '',
        keywords: '',
        status: '',
        countBeginTime: '',
        countEndTime: '',
      };
      this.resetForm("queryParams");
      this.handleQuery()
    },
    //编辑
    editClick(row) {
      this.dialogId = row.id
      this.dialogVisible = true;
      this.pathType = 'add'
    },
    configClick(row) {
      this.dialogId = row.id
      this.configVisible = true;
      this.pathType = 'add'
    },
    salesUpLoad() {
      this.importVisible = true;
      this.pathType = 'live'
    },
    writeOffUpLoad() {
      this.importVisible = true;
      this.pathType = 'order'
    },
    seeLiveDialog() {
      this.liveVisible = true;
    },
    seeOrderDialog() {
      this.orderVisible = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.countBeginTime != '') {
        this.statisticsDate = this.queryParams.countBeginTime + ' - ' + this.queryParams.countEndTime
      } else {
        this.statisticsDate = ''
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.reset();
    },
    _handleSuccess(e) {
      //成功回调
      this.resetQuery()
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '小计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.property == 'linkId' || column.property == 'merchantInfo' || column.property == 'liveId' || column.property == 'liveBeginTime'
          || column.property == 'liveEndTime' || column.property == 'customerName' || column.property == 'anchor' || column.property == 'centerControl'
        ) {
          sums[index] = '-';
        } else if (column.property == 'liveHour') {
          const values = data.map((item) => String(item[column.property]));
          if (values.length > 0) {
            const total = values.reduce((prev, curr) => {
              const value = curr;
              if (value != '') {
                const currs = curr.split(':');
                const currsH = currs[0] * 3600;
                const currsM = currs[1] * 60;
                const currsS = currs[2] * 1;
                return this.$cal.accAdd(parseFloat(prev), parseFloat(currsH + currsM + currsS));
              } else {
                return prev;
              }
            }, 0);
            var S = total % 60
            var M = parseInt(total % 3600 / 60);
            const H = parseInt(total / 3600)
            if (M < 10) {
              M = "0" + M
            }
            if (S < 10) {
              S = "0" + S
            } 
            sums[index] = H + ':' + M + ":" + S
          }
        } else if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return this.$cal.accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
          sums[index] += '';
        } else {
          sums[index] = '-';
        }
      });
      return sums;
    }
  }
};

</script>
<style scoped>
.head {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px 0;
}

.listBox {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px;
  margin-top: 10px;
}

.listBox_head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.img_box {
  width: 100px;
  height: 80px;
  border-radius: 4px;
}

.contentTitle {
  width: 100%;
  display: flex;
}
</style>